#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
char s[1005][1005];
int dis[1005][1005];
int a, b, c, d;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, m) cin >> s[i][j];
  cin >> a >> b >> c >> d;
  memset(dis, 0x3f, sizeof(dis));
  deque<pii> dq;
  dq.push_back(pii(a, b));
  dis[a][b] = 0;
  while (dq.size()) {
    pii now = dq.front();
    dq.pop_front();
    int x = now.first, y = now.second;
    rep(i, 0, 3) {
      int nx = x + dx[i], ny = y + dy[i];
      if (s[nx][ny] == '.' && dis[nx][ny] > dis[x][y]) {
        dq.push_front(pii(nx, ny));
        dis[nx][ny] = dis[x][y];
      }
    }
    rep(i, 0, 3) {
      rep(j, 1, 2) {
        int nx = x + j * dx[i], ny = y + j * dy[i];
        if (nx > 0 && ny > 0 && s[nx][ny] == '#' &&
            dis[nx][ny] > dis[x][y] + 1) {
          dq.push_back(pii(nx, ny));
          dis[nx][ny] = dis[x][y] + 1;
        }
      }
    }
  }
  cout << dis[c][d];
  return 0;
}